package com.lyy.yygh.hosp.controller.api;



import com.lyy.yygh.common.exception.YyghException;
import com.lyy.yygh.common.helper.HttpRequestHelper;
import com.lyy.yygh.common.result.Result;
import com.lyy.yygh.common.result.ResultCodeEnum;
import com.lyy.yygh.common.utils.MD5;
import com.lyy.yygh.hosp.service.DepartmentService;
import com.lyy.yygh.hosp.service.HospitalService;
import com.lyy.yygh.hosp.service.HospitalSetService;
import com.lyy.yygh.hosp.service.ScheduleService;
import com.lyy.yygh.model.hosp.Department;
import com.lyy.yygh.model.hosp.Hospital;
import com.lyy.yygh.model.hosp.Schedule;
import com.lyy.yygh.vo.hosp.DepartmentQueryVo;
import com.lyy.yygh.vo.hosp.ScheduleQueryVo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/hosp")
public class HospitalApiController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;



    //删除排班
    @PostMapping("schedule/remove")
    public Result removeschedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) paramap.get("hoscode");
        String hosScheduleId = (String) paramap.get("hosScheduleId");
        scheduleService.remove(hoscode,hosScheduleId);
        return Result.ok();

    }
    //查询排班
    @PostMapping("schedule/list")
    public Result getschedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);
        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramap.get("page")) ? 1
                : Integer.parseInt((String) paramap.get("page"));
        int limit = StringUtils.isEmpty(paramap.get("limit")) ? 1
                : Integer.parseInt((String) paramap.get("limit"));
        String hoscode = (String) paramap.get("hoscode");
        String depcode = (String) paramap.get("depcode");

        //签名校验


        //最终查询
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setHoscode(depcode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);

        return Result.ok(pageModel);

    }

    //添加排班
    @PostMapping("saveSchedule")
    public Result saveschedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);

        //签名校验
        scheduleService.saveschedule(paramap);
        return Result.ok();

    }
    //删除科室
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) paramap.get("hoscode");
        String depcode = (String) paramap.get("depcode");
        departmentService.remove(hoscode,depcode);
        return Result.ok();


    }
    //查询科室接口
    @PostMapping("department/list")
    public Result showdepartment(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);
        //当前页和每页记录数
        int page = StringUtils.isEmpty(paramap.get("page")) ? 1
                : Integer.parseInt((String) paramap.get("page"));
        int limit = StringUtils.isEmpty(paramap.get("limit")) ? 1
                : Integer.parseInt((String) paramap.get("limit"));
        String hoscode = (String) paramap.get("hoscode");

        //签名校验

        //最终查询
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);

        return Result.ok(pageModel);
    }

    //上传科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {

        //获取传递过来的科室信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);
        String hoscode = (String) paramap.get("hoscode");
        //获取医院系统传递过来的签名
        String sign = (String) paramap.get("sign");
        String signkey = hospitalSetService.getSignkey(hoscode);
        //把查出来的签名进行加密
        String signmd5 = MD5.encrypt(signkey);
        //判断签名是否一致
        if (!sign.equals(signmd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
        departmentService.save(paramap);
        return Result.ok();

    }

    //查询医院接口
    @PostMapping("hospital/show")
    public Result gethospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院编号
        String hoscode = (String) paramap.get("hoscode");
        //获取医院系统传递过来的签名
        String sign = (String) paramap.get("sign");

        String signkey = hospitalSetService.getSignkey(hoscode);

        //把查出来的签名进行加密
        String signmd5 = MD5.encrypt(signkey);
        //判断签名是否一致
        if (!sign.equals(signmd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }

        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);

    }

    //上传医院接口  到mongodb
    @PostMapping("saveHospital")
    public Result savehosp(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println("获取传递过来的医院信息"+parameterMap);
        Map<String, Object> paramap = HttpRequestHelper.switchMap(parameterMap);
        //获取医院系统传递过来的签名
        String sign = (String) paramap.get("sign");
        System.out.println("sign是"+sign);
        //获取医院系统的医院编号
        String hoscode = (String) paramap.get("hoscode");
        System.out.println("hocode是"+hoscode);
//        //根据医院编号查询数据库对应的签名
//        String signkey = hospitalSetService.getSignkey(hoscode);
//        //把查出来的签名进行加密
//        String signmd5 = MD5.encrypt(signkey);
//        //判断医院系统要调用的签名和数据库的签名是否一致，不一样就返回签名错误
//        if (!sign.equals(signmd5)) {
//            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
//        }
        //对图片的处理有一些意外，进行改正
        String logoData = (String) paramap.get("logoData");
//        logoData.replaceAll(" ", "+");
//        paramap.put("logoData", logoData);
        hospitalService.save(paramap);
        return Result.ok();

    }
}
